package cn.kys.modules.system.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.kys.common.exception.BadRequestException;
import cn.kys.common.utils.ThrowableUtil;
import cn.kys.config.DataScope;
import cn.kys.logging.aop.log.EnableKysLog;
import cn.kys.modules.system.domain.Dept;
import cn.kys.modules.system.domain.vo.Result;
import cn.kys.modules.system.pojo.GeometryPojo;
import cn.kys.modules.system.pojo.SubtaskGeom;
import cn.kys.modules.system.service.DeptService;
import cn.kys.modules.system.service.ImportShp2PgService;
import cn.kys.modules.system.service.dto.DeptDto;
import cn.kys.modules.system.service.dto.DeptQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.jdbc.JDBCDataStore;
import org.opengis.referencing.FactoryException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.*;

import static org.apache.commons.lang3.ArrayUtils.toArray;

/**
 * 功能描述：导入shp到postgis数据库
 *
 * @Auther:zxw
 */
@RestController
@Api(tags = {"业务——shp导入pg模块"})
@RequestMapping("/importShp2PgApi")
public class ImportShp2PgApi {


    @Autowired
    private ImportShp2PgService importShp2PgService;

    @EnableKysLog("导入shp到postgis数据库")
    @ApiOperation("导入shp到postgis数据库")
    @PostMapping(value = "/importShpToPG")

    public ResponseEntity importShp(@RequestParam("shpfile") MultipartFile[] shpfiles) throws FactoryException, IOException, SQLException {

        String tempShpFile = importShp2PgService.multipartFileShp2File(shpfiles);
        JDBCDataStore ds = importShp2PgService.getDataStore();
        SimpleFeatureSource simpleFeatureSource = importShp2PgService.readShp(new File(tempShpFile));
        importShp2PgService.createTable(ds, simpleFeatureSource);
        String tableName = importShp2PgService.shp2Table(ds, simpleFeatureSource);
        importShp2PgService.toMultipolygon(tableName);
        boolean isInsertTableDept = importShp2PgService.insertTableDept(tableName);
        return new ResponseEntity<>(isInsertTableDept, HttpStatus.OK);

    }


    @EnableKysLog("判断shp是否已经存在postgis数据库")
    @ApiOperation("判断shp是否已经存在postgis数据库")
    @PostMapping(value = "/shpHasImported")
    public ResponseEntity<Object> shpHasImported(@RequestParam("shpfile") MultipartFile[] shpfiles) throws Exception {

        if (shpfiles.length == 0) {
            return new ResponseEntity("请选择要上传的文件",HttpStatus.NO_CONTENT);
        }
        List<String> shpFileList=new ArrayList();
        String[] endWithStr={".shp",".shx",".dbf",".prj"};
    /*    List ednWithList= Arrays.asList(endWithStr);*/
        for (MultipartFile file : shpfiles) {
            if (file.isEmpty()) {
                return new ResponseEntity("文件不能为空",HttpStatus.NO_CONTENT);
            }
            String originalFilename = file.getOriginalFilename();
            shpFileList.add(originalFilename.substring(originalFilename.lastIndexOf("."),originalFilename.length()));
        }
        for (String str : endWithStr) {
           if(!shpFileList.contains(str)) {
               throw new Exception("上传文件不包含"+str+"文件");
           }
        }

        boolean is = importShp2PgService.shpHasImported(shpfiles);
        return new ResponseEntity<>(is, HttpStatus.OK);
    }


    @EnableKysLog("删除postgis数据库的表shp")
    @ApiOperation("删除postgis数据库的表shp")
    @GetMapping(value = "/delShpByName")
    public ResponseEntity<Object> delShpByName(@RequestParam("shpName") String shpName) {
        String is = importShp2PgService.delShpByName(shpName);
        return new ResponseEntity<>(is, HttpStatus.OK);
    }

    @EnableKysLog("新增点线面功能——加后直接进入到该任务的任务列表中")
    @ApiOperation("张所——新增点线面功能(加后直接进入到该任务的任务列表中)")
    @PostMapping (value = "/insertVectorByDraw")
    public Result insertVectorByDraw(@RequestBody SubtaskGeom subtaskGeom) {
        Integer keyId=importShp2PgService.insertVectorByDraw(subtaskGeom);
        return Result.success(keyId);
    }












}
